<?php
namespace app\common\controller;


use app\common\service\RbacSrv;
use think\Controller;
use think\Request;
use View;
use Db;
use Session;
use app\common\model\Menu;


class BaseController extends Controller
{

    protected $beforeActionList = [
        'checkLogin'=>  ['except'=>['login','imgcode','logindo','logout']],
        'checkPower'=>  ['except'=>['login','imgcode','logindo','logout']],
    ];


    public function __construct()
    {
        parent::__construct();


        $menu = new Menu();
        $menuDatas = $menu->getMenuRecursion();

        // 赋值全局模板变量
        View::share('menuDatas',$menuDatas);

    }


    public function checkPower(){
        $url = $this->request->url();
        $rbac = new RbacSrv();
        $isPower = $rbac->checkPower($url);

        $loginInfo  = Session::get('loginInfo');
        //如果是超级管理员就跳过权限验证
        if($loginInfo['uid'] != config('app.rbac_root_uid')){
            if(false === $isPower){
                return $this->error('你没有权限操作！');
                //exception('你没有权限操作！');
            }
        }



    }

    public function checkLogin(){

        $loginInfo  = Session::get('loginInfo');

        if(empty($loginInfo)){
            return $this->redirect('/admin/Common/login');
        }

        $viewData = [
            'loginInfo'=>$loginInfo
        ];

        View::share($viewData);

    }


}